package jcu.salt.io;

import java.io.Serializable;

/**
 * Transmittable
 * 
 * This class stores the sensor data and all related
 * metadata to transmit these via the middleware
 */
public class Transmittable implements Serializable {

	private static final long serialVersionUID = 1L;

	private long timestamp;
	private String dataType;
	private int sensorID;
	private String salID;
	private byte[] data;

	public Transmittable() {
		// to support default construction
	}

	public Transmittable(String salID, int sensorID) {
		this(salID, sensorID, null);
	}

	public Transmittable(String salID, int sensorID, String dataType) {
		this(salID, sensorID, dataType, null);
	}

	public Transmittable(String salID, int sensorID, String dataType, byte[] data) {
		this(salID,sensorID,dataType,data,-1);
	}
	
	public Transmittable(String salID, int sensorID, String dataType, byte[] data, long timestamp) {
		setSalID(salID);
		setSensorID(sensorID);
		setDataType(dataType);
		setData(data);
		setTimestamp(timestamp);		
	}

	@Override
	public boolean equals(Object obj) {
		if (!(obj instanceof Transmittable))
			return false;

		Transmittable other = ((Transmittable) obj);

		if (timestamp != other.timestamp)
			return false;

		if (dataType != other.dataType)
			return false;

		if (!salID.equals(other.salID))
			return false;

		if (sensorID != other.sensorID)
			return false;

		if (data != null && other.data != null) {

			if (data.length != other.data.length)
				return false;

			for (int i = 0; i < data.length; i++) {
				if (data[i] != other.data[i])
					return false;
			}
		} else {
			if(data == null && other.data != null)
				return false;
			
			if(data != null && other.data == null)
				return false;
		}
		return true;
	}

	public void setTimestamp(long timestamp) {
		if(timestamp==-1)
			this.timestamp = System.currentTimeMillis();
		else
			this.timestamp = timestamp;
	}

	public long getTimestamp() {
		return timestamp;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getDataType() {
		return dataType;
	}

	public void setSensorID(int sensorID) {
		this.sensorID = sensorID;
	}

	public int getSensorID() {
		return sensorID;
	}

	public void setSalID(String salID) {
		this.salID = salID;
	}

	public String getSalID() {
		return salID;
	}

	public void setData(byte[] data) {
		this.data = data;
	}

	public byte[] getData() {
		return data;
	}
	
	@Override
	public String toString() {
		return "[SAL=" + getSalID() + "][SENSOR=" + getSensorID() 
			+ "][TIME=" + getTimestamp() + "][TYPE=" + getDataType() + "] : "
			+ getData().length + " bytes";
	}
}
